package com.mall.b2c.controller;

import com.mall.b2c.service.GoodsService;
import com.mall.b2c.service.StoreGoodsService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Message;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.web.Servlets;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Misun on 2017/1/1.
 */
@Controller
@RequestMapping("/admin/b2c/storeGoods")
public class StoreGoodsController extends BaseController{

    @Resource
    private StoreGoodsService storeGoodsService;
    @Resource
    private GoodsService goodsService;

    @RequestMapping(value="/main", method= RequestMethod.GET)
    public String main(){
        return "/admin/b2c/storeGoods/main";
    }

    @RequestMapping(value="/tree", method=RequestMethod.GET)
    public String tree(){
        return "/admin/b2c/storeGoods/tree";
    }


    @RequestMapping(value="/list",method=RequestMethod.GET)
    public String list(Long storeId, ModelMap model){
        model.addAttribute("storeId", storeId);
        return "/admin/b2c/storeGoods/list";
    }

    @RequestMapping(value="/addGoods",method=RequestMethod.GET)
    public String addGoods(Long storeId, ModelMap model){
        model.addAttribute("storeId", storeId);
        return "/admin/b2c/storeGoods/addGoods";
    }

    /**
     * 获取商品列表信息
     */
    @RequestMapping(value="/queryGoodsData", method=RequestMethod.POST)
    @ResponseBody
    public Pager queryData(Pager pager, HttpServletRequest request){
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
       /*    pager.setFilterList(filters);

        Finder finder = Finder.create("from GoodsDomain bean where bean.id not in (:ids)");
        finder.setParamList("ids", )*/
        pager = goodsService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }

    @RequestMapping("/queryData")
    @ResponseBody
    public Pager queryData(Pager pager,Long storeId,HttpServletRequest request){
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
        filters.add(new SearchFilter("store.id", SearchFilter.Operator.EQ, storeId));
        pager = storeGoodsService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }


    @RequestMapping("/moveGoodsRight")
    @ResponseBody
    public Message moveGoodsRight(Long[] goodsIds, Long storeId){
        Message message = storeGoodsService.addGoods(goodsIds, storeId);
        if(message.getType() == Message.Type.ERROR) {
            throw new NoticeException(message.getContent());
        }
        return successMessage;
    }

}
